package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Role;
import com.qk.mapper.RoleMapper;
import com.qk.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Override
    public void add(Role role) {
        //补全role数据
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.insert(role);
    }

    @Override
    public PageResult<Role> list(String name, String label, Integer page, Integer pageSize) {

        //创建PageHelper对象,设置起始页和每页记录数
        PageHelper.startPage(page, pageSize);
        //调用mapper接口,执行分页查询
        List<Role> rolelist = roleMapper.selectList(name, label);
        //封装PageResult对象并返回
        Page<Role> p = (Page<Role>) rolelist;
        return new PageResult<>(p.getTotal(), p.getResult());
    }

    @Override
    public Role getById(Integer id) {
        return roleMapper.selectById(id);
    }

    @Override
    public void update(Role role) {
        //补全更新时间
        role.setUpdateTime(LocalDateTime.now());
        //执行SQL语句
        roleMapper.update(role);
    }

    @Override
    public void deleteById(Integer id) {
        roleMapper.deleteById(id);
    }

    @Override
    public List<Role> findAllList() {
        return roleMapper.selectAll();
    }
}
